
import * as UserUtils from './model/UserUtils';
import DebugUtils from './utils/DebugUtils';
import ExtendUtils from './utils/ExtendUtils';
import { getFunc } from './model/CacheUtils';
import * as Constants from './common/Constants';
import UmfLog from './utils/UmfLog';

App({
    onLaunch: function (options) {
        ExtendUtils.excute();

        this.init();
    },

    globalData: {
        states: {
            //是否处于登录过程
            isLoginProcess: false,
            //微信登录态
            wxLogin: true
        },

        //用户信息
        userData: {},
        //分页数
        pageSize: "20",
        //系统信息
        systemInfo: {},

        //用户维度缓存
        userCache: {},
        //代理商维度缓存
        agentCache: {}
    },

    init: function () {
        //初始化调试工具(生产环境不能打开调试工具)
        if(DebugUtils.isDebug()){
            DebugUtils.initConfig();
        }
        //获取用户数据
        this.globalData.userData = new UserUtils.userInfo().get();
        //获取用户缓存数据
        let cacheKey = this.globalData.userData.agentId + "-" + this.globalData.userData.userId;
        let userFuncCache = getFunc(cacheKey, Constants.Keys.USER_CACHE);
        this.globalData.userCache = userFuncCache? userFuncCache: {};
        //获取代理商维度缓存数据
        let agentCacheKey = this.globalData.userData.agentId;
        let agentFuncCache = getFunc(agentCacheKey, Constants.Keys.AGENT_CACHE);
        this.globalData.agentCache = agentFuncCache? agentFuncCache: {};
        //检查微信登录态
        this._checkWxSession();
        //获取系统信息
        this._getSystemInfo();

        //下载配置文件
        this._downloadConf();
    },

    /**
     * 检查登录状态
     * @private
     */
    checkLoginState: function (options, callback) {
        this.globalData.userData = new UserUtils.userInfo().get();

        //检查登录态为自动登录的场合
        if (options && options.loginToken) {
            this.globalData.states.isLoginProcess = true;
            callback && typeof callback.onAutoLogin === 'function' && callback.onAutoLogin(options.loginToken);
            return;
        }

        //检查登录态是手动登录的场合
        let checkUserData = this.globalData.userData.checkUserData();
        UmfLog.log("wxLogin:::" + this.globalData.states.wxLogin);
        UmfLog.log("hasChangePwd:::" + this.globalData.userData.oriPwdChange);
        UmfLog.log("checkUserData:::" + checkUserData);
        if (this.globalData.states.wxLogin
            && checkUserData) {
            this.globalData.states.isLoginProcess = false;
            callback && typeof callback.onSkipLogin === 'function' && callback.onSkipLogin();
        } else {
            this.globalData.states.isLoginProcess = true;
            callback && typeof callback.onLogin === 'function' && callback.onLogin();
        }
    },

    /**
     * 获取系统信息
     * @private
     */
    _getSystemInfo () {
        wx.getSystemInfo({
            success: res => {
                UmfLog.log("获取系统信息");
                UmfLog.log(res);
                this.globalData.systemInfo = res;
            }
        });
    },

    /**
     * 检查微信的登录态
     */
    _checkWxSession: function () {
        wx.checkSession({
            success: () => {
            },
            fail: () => {
                UmfLog.log("微信管理的登录态过期");
                this.globalData.states.wxLogin = false;
            }
        })
    },

    _downloadConf() {
        //TODO
    }
});